package com.ccp.dev.form.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.model.FormData;
import com.ccp.dev.form.model.FormField;
import com.ccp.dev.form.model.FormTable;
import com.ccp.dev.form.model.PkValue;
import com.ccp.dev.form.service.FormTableService;
import com.ccp.dev.workflow.bpmutil.FormDataUtil;
import com.ccp.dev.workflow.service.FormHandlerService;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 自定义表单数据处理
 *
 * @author zhaowj
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_FORM + "/formHandler")
@Action(ownermodel = SysAuditModelType.FORM_MANAGEMENT)
public class FormHandlerController extends SysBaseController {
    @Resource
    private FormHandlerService service;

    @Resource
    private FormTableService bpmFormTableService;


    /**
     * 保存业务数据
     *
     * @param request 请求
     * @return 保存结果
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @ResponseBody
    @Action(description = "在线表单保存数据", exectype = "添加日志",
            detail = "在线表单保存数据<#if success>成功<#else>失败</#if>")
    public ResultData save(HttpServletRequest request) {
        String data = RequestUtil.getString(request, "formData");
        String tableId = RequestUtil.getString(request, "tableId");
        String alias = RequestUtil.getString(request, "alias");
        FormTable bpmFormTable = bpmFormTableService.getByTableId(tableId, FormField.FIELD_NORMAL_HIDDEN);
        String id = RequestUtil.getString(request,"id");
        data = StringEscapeUtils.unescapeHtml4(data);
        try {
            if (StringUtil.isEmpty(id)) {
                FormData bpmFormData = FormDataUtil.parseJson(data, bpmFormTable);
                service.handFormData(bpmFormData, alias, data);
            } else {
                PkValue pkValue = new PkValue(bpmFormTable.getPkField(), id);
                pkValue.setIsAdd(false);
                FormData bpmFormData = FormDataUtil.parseJson(data, pkValue, bpmFormTable);
                service.handFormData(bpmFormData, alias, data);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("save.success");
        } catch (Exception ex) {
            ex.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("save.failure");
        }
    }
}
